cmake_minimum_required(VERSION 3.8)
project(usv_run)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(mavros_msgs REQUIRED)

# Include directories
include_directories(include)

# Create libraries
add_library(pid_controller src/pid_controller.cpp)
target_include_directories(pid_controller PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

# APM attitude control node
add_executable(attitude_control_node_apm 
  src/main_APM.cpp
  src/attitude_control_node_APM.cpp
)

target_include_directories(attitude_control_node_apm PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

ament_target_dependencies(attitude_control_node_apm
  rclcpp
  std_msgs
  geometry_msgs
  sensor_msgs
  mavros_msgs
)

target_link_libraries(attitude_control_node_apm pid_controller)

# PX4 attitude control node
add_executable(attitude_control_node_px4 
  src/main_PX4.cpp
  src/attitude_control_node_PX4.cpp
)

target_include_directories(attitude_control_node_px4 PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)

ament_target_dependencies(attitude_control_node_px4
  rclcpp
  std_msgs
  geometry_msgs
  sensor_msgs
  mavros_msgs
)

target_link_libraries(attitude_control_node_px4 pid_controller)

# Install executables
install(TARGETS
  attitude_control_node_apm
  attitude_control_node_px4
  DESTINATION lib/${PROJECT_NAME}
)

# Install libraries
install(TARGETS
  pid_controller
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

# Install header files
install(DIRECTORY include/
  DESTINATION include
)

# Install launch files
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

# Install config files
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}/
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()